package com.baor.utils;

import okhttp3.HttpUrl;

import java.io.*;
import java.security.*;
import java.util.Base64;

/**
 *  第一步：生成签名
 */
public class AuthorizationHeaderUtil {

    public static String getSign(String mchid, String method, HttpUrl url, String body, String serial_no,String nonceStr,long timestamp) throws IOException, SignatureException, NoSuchAlgorithmException, InvalidKeyException {

        String message = buildMessage(method, url, timestamp, nonceStr, body);
        String signature = sign(message.getBytes("utf-8"), mchid);
        return "mchid=\"" + mchid + "\","
                + "nonce_str=\"" + nonceStr + "\","
                + "timestamp=\"" + timestamp + "\","
                + "serial_no=\"" + serial_no + "\","
                + "signature=\"" + signature + "\"";
    }

    public static String getSign2(String mchId,String appid, String prepayid,String nonceStr,long timestamp) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
//        System.out.println(mchId);
//        System.out.println(appid);
//        System.out.println(prepayid);
//        System.out.println(nonceStr);
//        System.out.println(timestamp);
//        String msg = "appid=\"" + appid + "\","
//                + "nonce_str=\"" + nonceStr + "\","
//                + "timestamp=\"" + timestamp + "\","
//                + "prepay_id=\"" + prepay_id + "\","
//                + "signature=\"" + signature + "\"";
        String msg =  appid + "\n" + nonceStr + "\n" + timestamp + "\n" + "prepay_id=" + prepayid;
//        String msg = "appid=\"" + appid + "\","
//                + "nonce_str=\"" + nonceStr + "\","
//                + "timestamp=\"" + timestamp + "\","
//                + "prepay_id=\"" + prepayid + "\"";
        return sign(msg.getBytes(),mchId);
    }

    public static String sign(byte[] message, String mchid) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
        KeyPairFactory keyPairFactory = new KeyPairFactory();
        KeyPair tenpay_certificate = keyPairFactory.createPKCS12("apiclient_cert.p12", "Tenpay Certificate", mchid);
        Signature sign = Signature.getInstance("SHA256withRSA");
        sign.initSign(tenpay_certificate.getPrivate());
        sign.update(message);

        return Base64.getEncoder().encodeToString(sign.sign());
    }

    static String buildMessage(String method, HttpUrl url, long timestamp, String nonceStr, String body) {
        String canonicalUrl = url.encodedPath();
        if (url.encodedQuery() != null) {
            canonicalUrl += "?" + url.encodedQuery();
        }

        return method + "\n"
                + canonicalUrl + "\n"
                + timestamp + "\n"
                + nonceStr + "\n"
                + body + "\n";
    }

//    public static String token(String mchId, String nonceStr, long timestamp, String serialNo, String signature) {
//        final String TOKEN_PATTERN = "mchid=\"%s\",nonce_str=\"%s\",timestamp=\"%d\",serial_no=\"%s\",signature=\"%s\"";
//        // 生成tokenreturn
//        String.format(TOKEN_PATTERN,,nonceStr, timestamp, serialNo, signature);
//    }
//    public static void main(String[] args) {
//        System.out.println("40281a8188d95ea00188d95ea0220000".length());
//    }
}
